package com.witer.colorswitch.view;


import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

import com.witer.colorswitch.R;

public class ColorSwitchView extends ImageView {

    private Integer click_x = null;
    private Integer click_y = null;

    public int getClick_x() {
        return click_x;
    }

    public void setClick_x(int click_x) {
        this.click_x = click_x;
    }

    public int getClick_y() {
        return click_y;
    }

    public void setClick_y(int click_y) {
        this.click_y = click_y;
    }

    public ColorSwitchView(Context context) {
        super(context);
        init(context);
    }

    Bitmap bp;

    private void init(Context context) {

        bp = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.color_picker_knob);
        point_radius = ((int) (bp.getHeight() / 2));

        Bitmap imageBitmap = ((BitmapDrawable) getDrawable()).getBitmap();
        radius = imageBitmap.getHeight() / 2 - (int)(imageBitmap.getWidth() * ( 27.0 / 520.0));// 图片半径

    }

    public ColorSwitchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        int x, y;
        x = click_x == null ? getWidth() / 2 : click_x;
        y = click_y == null ? getHeight() / 2 : click_y;
        canvas.drawBitmap(bp, x - point_radius, y - point_radius, paint);

    }

    int center_x = 0;
    int center_y = 0;
    int point_radius = 0;
    int radius = 0;
    int border_width = 0;

    @SuppressLint("NewApi")
    public void setX_Y(int x, int y) {

//        if (point_radius == 0) {
//            Bitmap bp = BitmapFactory.decodeResource(getResources(),
//                    R.drawable.color_picker_knob);
//            point_radius = ((int) (bp.getHeight() / 2));
//            bp.recycle();
//        }


//        if (border_width == 0) {
//            border_width = (int) (getWidth() * .015);
////            radius = getWidth() / 2 - border_width;
//        }
        if (center_x == 0) {
            center_x = (int) (getWidth() / 2);
        }
        if (center_y == 0) {
            center_y = (int) (getHeight() / 2);
        }
        if (click_x == null) click_x = center_x;
        if (click_y == null) click_y = center_y;
        int be_x = Math.abs(x - center_x);
        int be_y = Math.abs(y - center_y);
        if (Math.sqrt(be_x * be_x + be_y * be_y) >= radius) {
            if (x == center_x) {
                click_x = x;
                click_y = y > center_y ? (radius * 2 + border_width) : border_width;
            } else if (y == center_y) {
                click_x = x > center_x ? (radius * 2 + border_width) : border_width;
                click_y = y;
            } else {
                CGPoint origin = new CGPoint(center_x, center_y);
                CGPoint tail = new CGPoint(x, y);    //

                CGPoint[] cgPoints = CGGeometryLib.getWhatIWanted(origin, tail, radius);
                if (cgPoints != null && cgPoints.length == 2) {
                    for (int i = 0; i < cgPoints.length; i++) {
                        if ((x - center_x) * (cgPoints[i].x - center_x) >= 0 && (y - center_y) * (cgPoints[i].y - center_y) >= 0) {
                            click_x = (int) cgPoints[i].x;
                            click_y = (int) cgPoints[i].y;
                            break;
                        }
                    }
                }
            }

        } else {
            click_x = x;
            click_y = y;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent motionEvent) {
        int color = 0;
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                float x1 = motionEvent.getX();
                float y1 = motionEvent.getY();
                setX_Y((int) x1, (int) y1);

                invalidate();
                if (onColorChangeListener != null) {
                    onColorChangeListener.onColorChange(getColor(getClick_x(), getClick_y()));
                }
                break;
            case MotionEvent.ACTION_UP:
                setX_Y((int) motionEvent.getX(), (int) motionEvent.getY());

                invalidate();
                if (onColorChangeListener != null) {
                    onColorChangeListener.onColorComplete(getColor(getClick_x(), getClick_y()));
                }
                break;
            case MotionEvent.ACTION_MOVE:
                try {
                    final float x = motionEvent.getX();
                    final float y = motionEvent.getY();
                    setX_Y((int) x, (int) y);
                    invalidate();
                    if (onColorChangeListener != null) {
                        onColorChangeListener.onColorChange(getColor(getClick_x(), getClick_y()));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
        return true;
    }

    /**
     * 获取当前选中点的颜色值
     *
     * @param x
     * @param y
     * @return
     */
    private int getColor(int x, int y) {
        Bitmap bitmap = ((BitmapDrawable) getDrawable()).getBitmap();
        int pixel = bitmap.getPixel(x, y);
        return pixel;
    }

    private static final String TAG = "ColorSwitchView";
    private OnColorViewChangeListener onColorChangeListener;


    public void setOnColorChangeListener(OnColorViewChangeListener onColorChangeListener) {
        this.onColorChangeListener = onColorChangeListener;
    }

    public interface OnColorViewChangeListener {
        /**
         * 颜色修改
         */
        void onColorChange(int color);

        /**
         * 修改颜色确认
         */
        void onColorComplete(int color);
    }
}
